package com.cheng.listener;

import java.awt.Graphics;
import java.awt.PrintJob;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.io.File;
import java.io.IOException;
import java.util.Properties;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;

import com.cheng.frame.CreateCodeFrame;
import com.cheng.frame.InputInfoFrame;
import com.cheng.util.DataUtil;


public class CreateCodeListener extends MouseAdapter implements ActionListener{
	private JButton bLast;
	private JButton bCreate;
	private JButton bKeep;
	private JButton bPrint;
	
	public CreateCodeListener(JButton bLast,JButton bCreate,JButton bKeep,JButton bPrint){
		this.bLast = bLast;
		this.bCreate = bCreate;
		this.bKeep = bKeep;
		this.bPrint = bPrint;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(e.getSource() == bLast){
			CreateCodeFrame.instance().setVisible(false);
			InputInfoFrame.instance().setVisible(true);
			InputInfoFrame.instance().requestFocus();
			InputInfoFrame.instance().show();
		}else if(e.getSource() == bCreate){
			int r = JOptionPane.showConfirmDialog(null,"确认信息无误需要制证么？","提示",JOptionPane.YES_NO_OPTION);
			if(r == 0){
				try {
					DataUtil.instance().CreateCode();
				} catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
				CreateCodeFrame.instance().getCodeViewPanel().repaint();
				CreateCodeFrame.instance().getJPanel1().setVisible(false);
				CreateCodeFrame.instance().getJPanel2().setVisible(true);
			}
		}else if(e.getSource() == bPrint){ 
			Toolkit kit = Toolkit.getDefaultToolkit(); //获取工具箱
		      Properties props = new Properties();
		      props.put("awt.print.printer", "durango"); //设置打印属性
		      props.put("awt.print.numCopies", "1");
		      if (kit != null) {
		         //获取工具箱自带的打印对象
		         PrintJob printJob = kit.getPrintJob(CreateCodeFrame.instance(), "Print View Frame", props);
		         if (printJob != null) {
		            Graphics pg = printJob.getGraphics(); //获取打印对象的图形环境
		            if (pg != null) {
		               try {
		                  CreateCodeFrame.instance().printAll(pg); //打印该窗体及其所有的组件
		               } finally {
		                  pg.dispose(); //注销图形环境
		               }
		            }
		            printJob.end(); //结束打印作业
		         }
		      }
		}else if(e.getSource() == bKeep){
			JFileChooser fileChooser = new JFileChooser();
			fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			int state = fileChooser.showOpenDialog(null);// 此句是打开文件选择器界面的触发语句  
            if (state == 1) {  
                return;  
            } else {  
                File f = fileChooser.getSelectedFile();// f为选择到的目录  
                String path = f.getAbsolutePath();  
                path  = path + "\\" + DataUtil.instance().getMs().split("/")[0];
                int r = DataUtil.instance().KeepCode(path);
                String message = "";
                if(r==1){
                	message = "保存成功！";
                }else{
                	message = "保存失败！";
                }
                JOptionPane.showMessageDialog(null, message);
            }  
		}
	}

}
